<?php
namespace app\common\search\jy;

/**
 * 提供搜索条件
 */
class Order extends Base{
	protected $request = 'jy\\Order';

	// 私有列表查询条件生成
	public function private_list($model, $post) {
		$model->removePrivacyField('transaction_id');
        $model->privacy();
        $model->append(['products', 'snapshot_address', 'order_logistics'])->where('delete', 0);
        $this->list_order($model, $post);
        $this->list_status($model, $post);
        $this->list_take_type($model, $post);
		return $model;
	}

	//店铺列表条件生成
    public function store_list($model, $post) {
        $model->where('delete', 0);
        $model->append(['products', 'snapshot_address', 'order_logistics', 'store_phone']);
        $model->where('sid', $post['sid']);
        $this->list_order($model, $post);
        $this->list_status($model, $post);
        $this->list_take_type($model, $post);
        return $model;
    }

    //添加排序条件
    public function list_order($model, $post) {
        if($post['order'] && $post['order_type']) {
            $model->order($post['order'] . ' ' . $post['order_type']);
        }
    }

    //添加状态相关搜索
    public function list_status($model, $post) {
        $status = $post['status'];
        $enum = loadEnum('OrderStatus');
        $statusOptions = $enum->get_options();
        if($status || $status === 0 || $status === '0') {
            if(isset($statusOptions[$status])) {
                $model->where('status', $status);
            } else {
                $arr = $enum->str2status($status);
                if($arr) {
                    $model->whereIn('status', $arr);
                }
            }
        } 
        
    }

    //添加提货方式搜索
    public function list_take_type($model, $post) {
        if($post['take_type']) {
            $model->where('take_type', $post['take_type']);
        }
        return $model;
    }
}
